//
//  ViewController.m
//  RunLoop
//
//  Created by user on 16/4/15.
//  Copyright © 2016年 user. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加观察者 监听runloop的状态
    /**
     kCFRunLoopEntry = (1UL << 0),1 进入
     kCFRunLoopBeforeTimers = (1UL << 1),2 时间
     kCFRunLoopBeforeSources = (1UL << 2),4 事件源
     kCFRunLoopBeforeWaiting = (1UL << 5),32 睡眠
     kCFRunLoopAfterWaiting = (1UL << 6),64 唤醒
     kCFRunLoopExit = (1UL << 7),128 退出
     kCFRunLoopAllActivities = 0x0FFFFFFFU
     */
    CFRunLoopObserverRef observer =CFRunLoopObserverCreateWithHandler(CFAllocatorGetDefault(), kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
        NSLog(@"-----%zd",activity);
    });
    CFRunLoopAddObserver(CFRunLoopGetCurrent(),observer, kCFRunLoopCommonModes);
    CFRelease(observer);
}
/**
 *cf内存管理（core foundation）
 ＊1:凡是带有create ，copy，retain等字眼的函数，创建出来的时候，都需要在最后做一次release
 release函数：cfrelease（）
 */
-(void)time1
{
    
    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
    
    //NSDefaultRunLoopMode:系统默认mode
    //UITrackingRunLoopMode：监听滑动mode scrollview
    /**
     *CommonModes:标记 占位 不是真正的模式包括（NSDefaultRunLoopMode,UITrackingRunLoopMode）
     */
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
}
-(void)time2
{
    //默认添加到defalutmode
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void)run
{
    
    NSLog(@"run");
}
@end
